#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

public:
    EchoServer(uint64_t port) : _server(port)
    {
        _server.SetThreadNum(2);
        _server.EnableInactiveRelease(10);
        _server.SetConnectedCallback(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _server.SetMessageCallback(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetCloseCallback(std::bind(&EchoServer::OnClosed, this, std::placeholders::_1));
    }

    void OnClosed(const PtrConnection &con)
    {
        // std::cout<<"连接断开"<<std::endl;
        LOG_DEBUG("Connection closed:%p", con.get());
    }

    void OnMessage(const PtrConnection &con, Buffer *buf)
    {
        LOG_DEBUG("recv data:%s", buf->current_read_pos());
        con->Send(buf->current_read_pos(), buf->readable_size());
        buf->move_read_pos(buf->readable_size());
        //    con->Shutdown();
    }

    void OnConnected(const PtrConnection &con)
    {
        // std::cout<<"连接成功"<<std::endl;
        LOG_DEBUG("New connection:%p", con.get());
    }

    void Start(){
        return _server.Start();
    }
};